<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="财务单据" prop="accountId">
        <el-select disabled :style="{width: '120px'}" v-model="queryParams.type" placeholder="财务单据" clearable>
          <el-option v-for="dict in fms_bill_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="账户" prop="accountId">
        <el-select :style="{width: '120px'}" v-model="queryParams.accountId" placeholder="请选择客户来源" clearable>
          <el-option v-for="dict in fms_account" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="单据编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item :label="queryParams.partnerName" prop="partnerId">
        <el-input
            v-model="queryParams.partnerId"
            placeholder="请输入交易单位"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="经手人" prop="handsPersonId">
        <el-input
          v-model="queryParams.handsPersonId"
          placeholder="请输入经手人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划时间" style="width: 308px">
        <el-date-picker
          v-model="daterangePlanTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="执行时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeExecutionTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['fms:bill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fms:bill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fms:bill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['fms:bill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="fms_bill_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="交易单位" align="center" prop="partnerId" />
      <el-table-column label="单据编号" align="center" prop="orderId" />
      <el-table-column label="经手人" align="center" prop="handsPersonId" />
      <el-table-column label="操作员" align="center" prop="userId" />
      <el-table-column label="合计金额" align="center" prop="totalMoney" />
      <el-table-column label="优惠金额" align="center" prop="discountMoney" />
      <el-table-column label="实收金额" align="center" prop="money" />
      <el-table-column label="账户(收款/付款)" align="center" prop="accountId" />
      <el-table-column label="计划时间" align="center" prop="planTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" align="center" prop="executionTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.executionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="sys_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="租户id" align="center" prop="tenantId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fms:bill:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fms:bill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务单据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="billRef" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="交易单位" prop="partnerId">
          <el-input v-model="form.partnerId" placeholder="请输入交易单位" />
        </el-form-item>
        <el-form-item label="单据编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="经手人" prop="handsPersonId">
          <el-input v-model="form.handsPersonId" placeholder="请输入经手人" />
        </el-form-item>
        <el-form-item label="操作员" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入操作员" />
        </el-form-item>
        <el-form-item label="合计金额" prop="totalMoney">
          <el-input v-model="form.totalMoney" placeholder="请输入合计金额" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountMoney">
          <el-input v-model="form.discountMoney" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="实收金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入实收金额" />
        </el-form-item>
        <el-form-item label="账户(收款/付款)" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账户(收款/付款)" />
        </el-form-item>
        <el-form-item label="计划时间" prop="planTime">
          <el-date-picker clearable
            v-model="form.planTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择计划时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="执行时间" prop="executionTime">
          <el-date-picker clearable
            v-model="form.executionTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
              v-for="dict in sys_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租户id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Bill">
import { listBill, getBill, delBill, addBill, updateBill } from "@/api/fms/bill";

const { proxy } = getCurrentInstance();
const { sys_state,fms_account,fms_bill_type } = proxy.useDict('sys_state','fms_account','fms_bill_type');

const billList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangePlanTime = ref([]);
const daterangeExecutionTime = ref([]);
const route = useRoute();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    typeName: null,
    partnerId: null,
    partnerName: '未知',
    orderId: null,
    handsPersonId: null,
    accountId: null,
    planTime: null,
    executionTime: null,
    tenantId: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询财务单据列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangePlanTime && '' != daterangePlanTime) {
    queryParams.value.params["beginPlanTime"] = daterangePlanTime.value[0];
    queryParams.value.params["endPlanTime"] = daterangePlanTime.value[1];
  }
  if (null != daterangeExecutionTime && '' != daterangeExecutionTime) {
    queryParams.value.params["beginExecutionTime"] = daterangeExecutionTime.value[0];
    queryParams.value.params["endExecutionTime"] = daterangeExecutionTime.value[1];
  }
  listBill(queryParams.value).then(response => {
    billList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: null,
    partnerId: null,
    orderId: null,
    handsPersonId: null,
    userId: null,
    totalMoney: null,
    discountMoney: null,
    money: null,
    accountId: null,
    planTime: null,
    executionTime: null,
    state: null,
    tenantId: null,
    remark: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    createTime: null
  };
  proxy.resetForm("billRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangePlanTime.value = [];
  daterangeExecutionTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加财务单据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBill(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改财务单据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["billRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBill(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBill(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除财务单据编号为"' + _ids + '"的数据项？').then(function() {
    return delBill(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('fms/bill/export', {
    ...queryParams.value
  }, `bill_${new Date().getTime()}.xlsx`)
}


/** 页面初始化 */
function initPage() {
  queryParams.value = route.query;
  if (queryParams.value.type == '1'){
    queryParams.value.partnerName = '客户'
  }else if (queryParams.value.type == '2'){
    queryParams.value.partnerName = '供应商'
  }else if (queryParams.value.type == '3'){
    queryParams.value.partnerName = '来往单位'
  }else if (queryParams.value.type == '4'){
    queryParams.value.partnerName = '来往单位'
  }

}

initPage();
getList();
</script>
